package com.feiyi.app.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.feiyi.app.R;
import com.feiyi.app.adapter.ArticleListAdapter;
import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseActivity;
import com.feiyi.app.bean.HomePageBean;
import com.feiyi.app.net.HttpMethods;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author wangliantao
 * @time 2018 11 2018/11/18 3:34 AM
 * @email 1346947404@qq.com
 * @package com.feiyi.app.activity
 * @describe： In me the tiger sniffs the rose
 */
public class ArticleListActivity extends BaseActivity implements OnRefreshListener {

    Unbinder unbinder;

    @BindView(R.id.tv_title_titlebar)
    TextView tv_title;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    private List<HomePageBean.DataBean.ArticlesBean> mList = new ArrayList<>();

    private ArticleListAdapter mAdapter;


    private static final String TAG = "main";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        unbinder = ButterKnife.bind(this);

        initView();
        initData();
    }

    private void initView() {
        tv_title.setText("最新快报");
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setOnRefreshListener(this);
        mAdapter = new ArticleListAdapter(R.layout.item_article_list,mList);
        recyclerView.setAdapter(mAdapter);
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("article_id",mList.get(position).getArticle_id());
                ActivityUtils.startActivity(bundle,ArticleDetailActivity.class);
            }
        });

    }

    private void initData() {
        HttpMethods.getInstance().info.getHomePage1(AppConstant.HOME_URL)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomePageBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(HomePageBean homePageBean) {

                        if(null != homePageBean.getData().getArticles())
                            mList.addAll(homePageBean.getData().getArticles());


                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: "+e.toString() );
                        ToastUtils.showShort("获取网络数据失败");
                    }

                    @Override
                    public void onComplete() {

                        mAdapter.setNewData(mList);
                        mAdapter.notifyDataSetChanged();
                    }
                });


    }

    @OnClick({R.id.iv_back_titlebar})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back_titlebar:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh(3000);
    }
}
